﻿Public Delegate Sub DeleUpdateSetting()

Public Class channelSetting

    Private _confirmHandler As DeleUpdateSetting
    Private _chs() As AdChannel

    ''' <summary>
    ''' 构造函数
    ''' </summary>
    ''' <param name="handler">设置完成后的回调</param>
    ''' <param name="chs">设备的通道列表</param>
    ''' <remarks></remarks>
    Public Sub New(handler As DeleUpdateSetting, ByRef chs() As AdChannel)

        ' 此调用是设计器所必需的。
        InitializeComponent()
        _confirmHandler = handler
        _chs = chs
        ' 在 InitializeComponent() 调用之后添加任何初始化。

    End Sub

    Private Sub lbAi0Color_Click(sender As Object, e As EventArgs) Handles lbAi0Color.Click, lbAi1Color.Click, lbAi2Color.Click
        If colorAis.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
            sender.BackColor = colorAis.Color
        End If
    End Sub

    Private Sub btnOK_Click(sender As Object, e As EventArgs) Handles btnOK.Click
        Me.Hide()
        SaveChannelSetting()
        Me.Invoke(_confirmHandler)
    End Sub

    Private Sub channelSetting_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Me.ActiveControl = btnOK
    End Sub

    Private Sub btnCancel_Click(sender As Object, e As EventArgs) Handles btnCancel.Click
        Me.Hide()
    End Sub

    ''' <summary>
    ''' 展示当前的配置信息
    ''' </summary>
    ''' <remarks></remarks>
    Public Sub ShowChannelSetting()
        If _chs(0) IsNot Nothing Then
            GetChannelSetting(groupAi0, _chs(0))
        End If
        If _chs(1) IsNot Nothing Then
            GetChannelSetting(groupAi1, _chs(1))
        End If
        If _chs(2) IsNot Nothing Then
            GetChannelSetting(groupAi2, _chs(2))
        End If
    End Sub

    ''' <summary>
    ''' 保存设置好的通道配置
    ''' </summary>
    ''' <remarks></remarks>
    Public Sub SaveChannelSetting()
        If _chs(0) IsNot Nothing Then
            SetChannelSetting(groupAi0, _chs(0))
        End If
        If _chs(1) IsNot Nothing Then
            SetChannelSetting(groupAi1, _chs(1))
        End If
        If _chs(2) IsNot Nothing Then
            SetChannelSetting(groupAi2, _chs(2))
        End If
    End Sub

    ''' <summary>
    ''' 将ch中的参数展示到group的控件上
    ''' </summary>
    ''' <param name="group">展示参数的控件</param>
    ''' <param name="ch">需展示的数据</param>
    ''' <remarks></remarks>
    Public Sub GetChannelSetting(ByRef group As GroupBox, ByVal ch As AdChannel)
        Dim index As Integer
        index = group.Tag
        For Each txt_box As Object In group.Controls
            If txt_box.GetType().Name = "TextBox" Then
                If txt_box.Name.Contains("Ratio") Then txt_box.Text = ch.ratio
                If txt_box.Name.Contains("Max") Then txt_box.Text = ch.max
                If txt_box.Name.Contains("Min") Then txt_box.Text = ch.min
                If txt_box.Name.Contains("Width") Then txt_box.Text = ch.lineWidth
                If txt_box.Name.Contains("Brief") Then txt_box.Text = ch.brief
                If txt_box.Name.Contains("Offset") Then txt_box.Text = ch.offset
            End If
            If txt_box.Name.Contains("Color") Then txt_box.BackColor = ch.lineColor
        Next
    End Sub

    ''' <summary>
    ''' 将goup上的数据存储到通道配置中
    ''' </summary>
    ''' <param name="group"></param>
    ''' <param name="ch"></param>
    ''' <remarks></remarks>
    Public Sub SetChannelSetting(ByVal group As GroupBox, ByRef ch As AdChannel)
        For Each txt_box As Object In group.Controls
            If txt_box.GetType().Name = "TextBox" Then
                If txt_box.Name.Contains("Ratio") Then ch.ratio = txt_box.Text
                If txt_box.Name.Contains("Max") Then ch.max = txt_box.Text
                If txt_box.Name.Contains("Min") Then ch.min = txt_box.Text
                If txt_box.Name.Contains("Width") Then ch.lineWidth = txt_box.Text
                If txt_box.Name.Contains("Brief") Then ch.brief = txt_box.Text
                If txt_box.Name.Contains("Offset") Then ch.offset = txt_box.Text
            End If
            If txt_box.Name.Contains("Color") Then ch.lineColor = txt_box.BackColor
        Next
    End Sub
End Class